using System.Dynamic;
using TestWebApi.IServices;
using TestWebApi.Models.MySql;
using TestWebApi.Util;

namespace TestWebApi.Services;

public class DataService: IDataService
{
    
    public DatabaseContext DbContext;
    
    public DataService(DatabaseContext dbContext)
    {
        this.DbContext = dbContext;
    }
    /// <summary>
    /// 查询某个表特定时间段的数据
    /// </summary>
    /// <param name="connectionName">数据库连接名称</param>
    /// <param name="tableName">表名</param>
    /// <param name="startTime">开始时间</param>
    /// <param name="endTime"></param>
    /// <returns></returns>
    public List<DefaultModel> GetMySqlData(string connectionName, string tableName, DateTime startTime, DateTime endTime)
    {
        dynamic rowTips = new ExpandoObject();
        var rowDictTips = (IDictionary<string, object>)rowTips;
        try
        {
          var currentDbcontext = DbContext.GetDbContext(connectionName);
          // 判断数据库中是否包含指定表名的表
          var isTableExist = currentDbcontext.DbMaintenance.IsAnyTable(tableName);
          if (isTableExist)
              return currentDbcontext.Queryable<DefaultModel>(tableName)
                  .Where(x => x.Time >= startTime && x.Time <= endTime)
                  .ToList();
          AppLogger.Information($"表不存在{tableName}");
          rowDictTips["提示"] = $"表不存在{tableName}";
          return rowTips.ToList();
        }
        catch (Exception e)
        {
          AppLogger.Error(e, "获取数据失败: {Message}", e.Message);
           rowDictTips["提示"] = $"获取数据失败{e.Message}";
           return rowTips.ToList();
        }   
    }
    /// <summary>
    /// 获取Mysql某个表特定时间段数据(动态列表)
    /// </summary>
    /// <param name="connectionName">数据库连接名称</param>
    /// <param name="tableName">表名</param>
    /// <param name="startTime">开始时间</param>
    /// <param name="endTime">结束时间</param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public List<dynamic> GetMySqlDataByDynamic(string connectionName, string tableName, DateTime startTime, DateTime endTime)
    {
        dynamic rowTips = new ExpandoObject();
        var rowDictTips = (IDictionary<string, object>)rowTips;
        try
        {
          var currentDbcontext = DbContext.GetDbContext(connectionName);
          var isTableExist = currentDbcontext.DbMaintenance.IsAnyTable(tableName);
          if (isTableExist)
              return currentDbcontext.Queryable<dynamic>().AS(tableName)
                  .Where($"{tableName}.time >= @startTime and {tableName}.Time <= @endTime",
                      new { startTime = startTime, endTime = endTime }).ToList();
   
          rowDictTips["提示"] = $"表不存在{tableName}";
          AppLogger.Information($"表不存在{tableName}");
          return rowTips.ToList();
            
        }
        catch (Exception e)
        {
           AppLogger.Error(e, "获取数据失败: {Message}", e.Message);
            rowDictTips["提示"] = $"获取数据失败{e.Message}";
            return rowTips.ToList();
        }
    }
}
